package two.chapter_1.c1_1;

import edu.princeton.cs.algs4.StdOut;
import one.chapter_1.Stopwatch;

public class Ex1_1_19 {

    public static void main(String[] args) {

        Stopwatch stopwatch = new Stopwatch();
        long val = 0;
        for (int N = 0; N < 50; N++) {
            val = betterF(N);
            StdOut.println(N + " " + val);

        }
        double perTime = stopwatch.elapsedTime() / val;
        StdOut.println("执行一次所需时间：" + perTime);//  N=43时 ,1.4044*10^-8   N=47时，1.3469*10^-8   N=49时 1.41089*10^-8


        long n= (long) (3600/( 1.4*Math.pow(10,-8)));
        StdOut.println("n:"+n);//257142857142
    }


    public static long F(int N) {
        if (N == 0) return 0;
        if (N == 1) return 1;
        return F(N - 1) + F(N - 2);
    }

    public static long a[] = new long[3];

    /**
     * 优化版本
     */

    public static long betterF(int N) {
        if (N == 0) {
            a[0] = 0;
            return a[0];
        }

        if (N == 1) {
            a[1] = 1;
            return a[1];
        }

        a[2] = a[0] + a[1];
        a[0] = a[1];
        a[1] = a[2];
        return a[2];

    }

}
